<?php

use App\Role;
use Illuminate\Database\Seeder;

class PermissionTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //关闭外键约束
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');

        //清空权限相关的数据表
        \App\Permission::truncate();
        \App\Role::truncate();
        \App\User::truncate();
        DB::table('role_user')->delete();
        DB::table('permission_role')->delete();
        DB::table('cinemas')->delete();


        //开启外键约束
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');

        /**
         * 创建两个数组，方便给角色批量添加
         */
        $super_admin = [];
        $cinema_admin = [];

        /**
         * 创建权限
         */

        //前台售票
        $sale_ticket = new \App\Permission();
        $sale_ticket->name = 'sale_ticket';
        $sale_ticket->pid = 0;
        $sale_ticket->display_name = '前台售票';
        $sale_ticket->description = 1;
        $sale_ticket->save();
        $cinema_admin[] = $sale_ticket->id;
            //购票
            $buy_ticket = new \App\Permission();
            $buy_ticket->name = 'buy_ticket';
            $buy_ticket->pid = $sale_ticket->id;
            $buy_ticket->display_name = '前台购票';
            $buy_ticket->description = 2;
            $buy_ticket->save();
            $cinema_admin[] = $buy_ticket->id;
            //验票
            $check_ticket = new \App\Permission();
            $check_ticket->name = 'check_ticket';
            $check_ticket->pid = $sale_ticket->id;
            $check_ticket->display_name = '前台验票';
            $check_ticket->description = 2;
            $check_ticket->save();
            $cinema_admin[] = $check_ticket->id;
            //前端退票
            $sale_refund_ticket = new \App\Permission();
            $sale_refund_ticket->name = 'sale_refund_ticket';
            $sale_refund_ticket->pid = $sale_ticket->id;
            $sale_refund_ticket->display_name = '前台退票';
            $sale_refund_ticket->description = 2;
            $sale_refund_ticket->save();
            $cinema_admin[] = $sale_refund_ticket->id;
            //前端打印
            $sale_printer_ticket = new \App\Permission();
            $sale_printer_ticket->name = 'sale_printer_ticket';
            $sale_printer_ticket->pid = $sale_ticket->id;
            $sale_printer_ticket->display_name = '前台打印';
            $sale_printer_ticket->description = 2;
            $sale_printer_ticket->save();
            $cinema_admin[] = $sale_printer_ticket->id;

        //后台管理
        $admin_ticket = new \App\Permission();
        $admin_ticket->name = 'admin_ticket';
        $admin_ticket->pid = 0;
        $admin_ticket->display_name = '后台管理';
        $admin_ticket->description = 1;
        $admin_ticket->save();
        $cinema_admin[] = $admin_ticket->id;
        $super_admin[] = $admin_ticket->id;

            //放映管理
            $session_manage = new \App\Permission();
            $session_manage->name = 'session_manage';
            $session_manage->pid = $admin_ticket->id;
            $session_manage->display_name = '放映管理';
            $session_manage->description = 1;
            $session_manage->save();
            $cinema_admin[] = $session_manage->id;

                //计划排片
                $layout_film = new \App\Permission();
                $layout_film->name = 'layout_film';
                $layout_film->pid = $session_manage->id;
                $layout_film->display_name = '计划排片';
                $layout_film->description = 2;
                $layout_film->save();
                $cinema_admin[] = $layout_film->id;
                //计划管理
                $session_film = new \App\Permission();
                $session_film->name = 'session_film';
                $session_film->pid = $session_manage->id;
                $session_film->display_name = '场次管理';
                $session_film->description = 2;
                $session_film->save();
                $cinema_admin[] = $session_film->id;
                //计划发售
                $showtime_schedule_manager = new \App\Permission();
                $showtime_schedule_manager->name = 'showtime_schedule_manager';
                $showtime_schedule_manager->pid = $session_manage->id;
                $showtime_schedule_manager->display_name = '场次发售';
                $showtime_schedule_manager->description = 2;
                $showtime_schedule_manager->save();
                $cinema_admin[] = $showtime_schedule_manager->id;
            //影片管理
            $film_manager = new \App\Permission();
            $film_manager->name = 'film_manager';
            $film_manager->pid = $admin_ticket->id;
            $film_manager->display_name = '影片管理';
            $film_manager->description = 1;
            $film_manager->save();
            $cinema_admin[] = $film_manager->id;
            $super_admin[] = $film_manager->id;

                //影片下载
                $zhuanzi_film = new \App\Permission();
                $zhuanzi_film->name = 'zhuanzi_film';
                $zhuanzi_film->pid = $film_manager->id;
                $zhuanzi_film->display_name = '影片下载';
                $zhuanzi_film->description = 2;
                $zhuanzi_film->save();
                $super_admin[] = $zhuanzi_film->id;

                //本地影片
                $local_film = new \App\Permission();
                $local_film->name = 'local_film';
                $local_film->pid = $film_manager->id;
                $local_film->display_name = '影片库';
                $local_film->description = 2;
                $local_film->save();
                $super_admin[] = $local_film->id;
                $cinema_admin[] = $local_film->id;


            //影院信息管理
            $cinema_manager = new \App\Permission();
            $cinema_manager->name = 'cinema_manager';
            $cinema_manager->pid = $admin_ticket->id;
            $cinema_manager->display_name = '影院管理';
            $cinema_manager->description = 1;
            $cinema_manager->save();
            $super_admin[] = $cinema_manager->id;
            $cinema_admin[] = $cinema_manager->id;


                //影院列表管理
                $cinema_list = new \App\Permission();
                $cinema_list->name = 'cinema_list';
                $cinema_list->pid = $cinema_manager->id;
                $cinema_list->display_name = '所有影院';
                $cinema_list->description = 2;
                $cinema_list->save();
                $super_admin[] = $cinema_list->id;

                //完善影院信息
                $add_cinema = new \App\Permission();
                $add_cinema->name = 'add_cinema';
                $add_cinema->pid = $cinema_manager->id;
                $add_cinema->display_name = '基本信息';
                $add_cinema->description = 2;
                $add_cinema->save();
                $cinema_admin[] = $add_cinema->id;

                //影厅管理
                $screen_manager = new \App\Permission();
                $screen_manager->name = 'screen_manager';
                $screen_manager->pid = $cinema_manager->id;
                $screen_manager->display_name = '影厅管理';
                $screen_manager->description = 2;
                $screen_manager->save();
                $cinema_admin[] = $screen_manager->id;
            //票务管理
            $ticket_manager = new \App\Permission();
            $ticket_manager->name = 'ticket_manager';
            $ticket_manager->pid = $admin_ticket->id;
            $ticket_manager->display_name = '票务管理';
            $ticket_manager->description = 1;
            $ticket_manager->save();
            $cinema_admin[] = $ticket_manager->id;

                //验票
                $admin_check_ticket = new \App\Permission();
                $admin_check_ticket->name = 'admin_check_ticket';
                $admin_check_ticket->pid = $ticket_manager->id;
                $admin_check_ticket->display_name = '验票';
                $admin_check_ticket->description = 2;
                $admin_check_ticket->save();
                $cinema_admin[] = $admin_check_ticket->id;

                //退票管理
                $admin_refund_ticket = new \App\Permission();
                $admin_refund_ticket->name = 'admin_refund_ticket';
                $admin_refund_ticket->pid = $ticket_manager->id;
                $admin_refund_ticket->display_name = '退票管理';
                $admin_refund_ticket->description = 2;
                $admin_refund_ticket->save();
                $cinema_admin[] = $admin_refund_ticket->id;


                //补登管理
                $admin_bound_ticket = new \App\Permission();
                $admin_bound_ticket->name = 'admin_bound_ticket';
                $admin_bound_ticket->pid = $ticket_manager->id;
                $admin_bound_ticket->display_name = '补登管理';
                $admin_bound_ticket->description = 2;
                $admin_bound_ticket->save();
                $cinema_admin[] = $admin_bound_ticket->id;

                //票价方案
                $ticket_plan_manager = new \App\Permission();
                $ticket_plan_manager->name = 'ticket_plan_manager';
                $ticket_plan_manager->pid = $ticket_manager->id;
                $ticket_plan_manager->display_name = '票价方案';
                $ticket_plan_manager->description = 2;
                $ticket_plan_manager->save();
                $cinema_admin[] = $ticket_plan_manager->id;

            //统计报表
            $statistic_report = new \App\Permission();
            $statistic_report->name = 'statistic_report';
            $statistic_report->pid = $admin_ticket->id;
            $statistic_report->display_name = '统计报表';
            $statistic_report->description = 1;
            $statistic_report->save();
            $super_admin[] = $statistic_report->id;
            $cinema_admin[] = $statistic_report->id;
                //综合票务明细表
                $synthetical_ticket_table = new \App\Permission();
                $synthetical_ticket_table->name = 'synthetical_ticket_table';
                $synthetical_ticket_table->pid = $statistic_report->id;
                $synthetical_ticket_table->display_name = '综合票务明细表';
                $synthetical_ticket_table->description = 2;
                $synthetical_ticket_table->save();
                $super_admin[] = $synthetical_ticket_table->id;
                $cinema_admin[] = $synthetical_ticket_table->id;
                //放映计划表
                $showtime_schedule_table = new \App\Permission();
                $showtime_schedule_table->name = 'showtime_schedule_table';
                $showtime_schedule_table->pid = $statistic_report->id;
                $showtime_schedule_table->display_name = '放映计划表';
                $showtime_schedule_table->description = 2;
                $showtime_schedule_table->save();
                $super_admin[] = $showtime_schedule_table->id;
                $cinema_admin[] = $showtime_schedule_table->id;
                //场次排片表
                $session_table = new \App\Permission();
                $session_table->name = 'session_table';
                $session_table->pid = $statistic_report->id;
                $session_table->display_name = '场次排片表';
                $session_table->description = 2;
                $session_table->save();
                $super_admin[] = $session_table->id;
                $cinema_admin[] = $session_table->id;
                //影院票房上报表
                $settlement_report_table = new \App\Permission();
                $settlement_report_table->name = 'settlement_report_table';
                $settlement_report_table->pid = $statistic_report->id;
                $settlement_report_table->display_name = '影院票房上报表';
                $settlement_report_table->description = 2;
                $settlement_report_table->save();
                $super_admin[] = $settlement_report_table->id;
                $cinema_admin[] = $settlement_report_table->id;

            //系统管理
            $system_manager = new \App\Permission();
            $system_manager->name = 'data_manager';
            $system_manager->pid = $admin_ticket->id;
            $system_manager->display_name = '系统管理';
            $system_manager->description = 1;
            $system_manager->save();
            $super_admin[] = $system_manager->id;
            $cinema_admin[] = $system_manager->id;
                //用户管理
                $user_manager = new \App\Permission();
                $user_manager->name = 'user_manager';
                $user_manager->pid = $system_manager->id;
                $user_manager->display_name = '用户管理';
                $user_manager->description = 2;
                $user_manager->save();
                $super_admin[] = $user_manager->id;
                $cinema_admin[] = $user_manager->id;
                //角色管理
                $roles_manager = new \App\Permission();
                $roles_manager->name = 'roles_manager';
                $roles_manager->pid = $system_manager->id;
                $roles_manager->display_name = '角色管理';
                $roles_manager->description = 2;
                $roles_manager->save();
                $super_admin[] = $roles_manager->id;
                $cinema_admin[] = $roles_manager->id;
                //数据上报
                $data_report = new \App\Permission();
                $data_report->name = 'data_report';
                $data_report->pid = $system_manager->id;
                $data_report->display_name = '数据上报';
                $data_report->description = 2;
                $data_report->save();
                $cinema_admin[] = $data_report->id;
                //备份管理
                $backup_download = new \App\Permission();
                $backup_download->name = 'backup_download';
                $backup_download->pid = $system_manager->id;
                $backup_download->display_name = '备份管理';
                $backup_download->description = 2;
                $backup_download->save();
                $super_admin[] = $backup_download->id;
                //日志管理
                $log_manage = new \App\Permission();
                $log_manage->name = 'log_manage';
                $log_manage->pid = $system_manager->id;
                $log_manage->display_name = '系统日志';
                $log_manage->description = 2;
                $log_manage->save();
                $super_admin[] = $log_manage->id;
                $cinema_admin[] = $log_manage->id;
                //软件版本
                $software_version = new \App\Permission();
                $software_version->name = 'software_version';
                $software_version->pid = $system_manager->id;
                $software_version->display_name = '软件版本';
                $software_version->description = 2;
                $software_version->save();
                $super_admin[] = $software_version->id;
                $cinema_admin[] = $software_version->id;
            //合作管理
            $cooperative_manager = new \App\Permission();
            $cooperative_manager->name = 'cooperative_manager';
            $cooperative_manager->pid = $admin_ticket->id;
            $cooperative_manager->display_name = '合作管理';
            $cooperative_manager->description = 1;
            $cooperative_manager->save();
            $super_admin[] = $cooperative_manager->id;
            $cinema_admin[] = $cooperative_manager->id;
                //网络代售商设置
                $cinema_online_ticketer = new \App\Permission();
                $cinema_online_ticketer->name = 'cinema_online_ticketer';
                $cinema_online_ticketer->pid = $cooperative_manager->id;
                $cinema_online_ticketer->display_name = '网络代售商设置';
                $cinema_online_ticketer->description = 2;
                $cinema_online_ticketer->save();
                $cinema_admin[] = $cinema_online_ticketer->id;
                //网络代售商
                $online_ticketer_list = new \App\Permission();
                $online_ticketer_list->name = 'online_ticketer_list';
                $online_ticketer_list->pid = $cooperative_manager->id;
                $online_ticketer_list->display_name = '网络代售商';
                $online_ticketer_list->description = 2;
                $online_ticketer_list->save();
                $super_admin[] = $online_ticketer_list->id;

            $super = new Role();
            $super->name = '超级管理员';
            $super->type = 1;
            $super->status = 0;
            $super->save();
            $super->perms()->sync($super_admin);

            $cinema_role = new Role();
            $cinema_role->name = '影院管理员';
            $cinema_role->type = 2;
            $cinema_role->status = 0;
            $cinema_role->save();
            $cinema_role->perms()->sync($cinema_admin);


    }
}
